<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 个人中心
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="账号名">
                        <el-input v-model="ruleForm.name" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="身份">
                        <el-input v-model="ruleForm.identity" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    </el-form-item>
                </el-form>


            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'Application',
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            
            ruleForm: {
                name: '',
                identity: '',
                pass: '',
                checkPass: '',
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ]
            }
        };
    },
     created() {
        let ind = localStorage.getItem('manage_username');
        this.ruleForm.name = ind;
        if(ind == 'admin') {
             this.ruleForm.identity = '管理员'
        }
    },
    methods: {
        submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              this.submit();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      submit() {
            let that = this;
            let params = {
                "email": "",
                "id": localStorage.getItem('ms_userId'),
                "mac": "",
                "password": this.ruleForm.pass,
                "phone": "",
                "status": 0,
                "uname": this.ruleForm.name
            }
            console.log('params',params)
            const url = '/sysmodule/vManageUserInfo/editManage';
            const headers = {
                'Content-Type': 'application/json;charset=UTF-8',
                'X-cloud-Token': localStorage.getItem('ms_token')
            };
            this.$axios.post(url, params, {headers}).then((res) => {
                
                if(res.data.code == 200){
                    localStorage.removeItem('manage_username');
                    localStorage.removeItem('ms_userId');
                    localStorage.removeItem('ms_token');
                    this.$router.push('/login');
                    console.log('res.data',res.data)
                }else{
                    this.$message.error('操作失败');
                };
            }).catch((error) => {
                console.warn(error);
            });
      }
    }
};
</script>